using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;

namespace CustomItem.Commands.CustomItemCommands
{
    public class Spawn : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.GivingItems))
            {
                response = "你没有权限使用此命令！";
                return false;
            }

            if (arguments.Count < 2)
            {
                response = "用法: customitem spawn 玩家ID 物品类名";
                return false;
            }

            if (!int.TryParse(arguments.At(0), out int id))
            {
                response = "请输入一个正确的数字！";
                return false;
            }

            if (!Player.TryGet(id, out var player))
            {
                response = "无法找到指定的玩家！";
                return false;
            }

            Type type = CustomItemPlugin.Instance.Manager.AllTypes.Keys.FirstOrDefault(x => x.Name == arguments.At(1));
            if (type == null)
            {
                response = "无法找到指定的物品，请输入customitem list查询";
                return false;
            }

            CustomItem.Spawn(type, player.Position);
            response = "已在该玩家脚下生成指定物品";
            return true;
        }

        public string Command { get; } = "spawn";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "在脚下生成插件道具";
    }
}